package com.tjetc.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tjetc.dao.DepartmentDao;
import com.tjetc.dao.DoctorDao;
import com.tjetc.dao.impl.DepartmentDaoImpl;
import com.tjetc.dao.impl.DoctorDaoImpl;
import com.tjetc.domain.Department;
import com.tjetc.domain.StaffsCount;
import com.tjetc.domain.News;
import com.tjetc.service.DepartmentService;
import com.tjetc.utils.JedisDb;
import com.tjetc.utils.PageInfo;
import redis.clients.jedis.Jedis;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DepartmentServiceImpl implements DepartmentService {
    private DepartmentDao departmentDao = new DepartmentDaoImpl();
    private DoctorDao doctorDao = new DoctorDaoImpl();

    private Jedis jedis = JedisDb.jedis();
    @Override
    public int getCount(String name) {
        try {
            return departmentDao.getCount(name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Department> list(String name, PageInfo<Department> pageInfo) {
        try {
            List<Department> list = departmentDao.list(name, pageInfo);
            for (Department department : list) {
                department.setDoctor(doctorDao.getById(department.getDid()));//这是设置主任医生
                if (department.getParent_id()>0){//判断是否有父科室
                    department.setPName(departmentDao.queryById(department.getParent_id()).getName());
                }
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean add(Department department) {
        try {
            departmentDao.add(department);
            List<Department> list=departmentDao.queryPidNotZero();
            for (Department department1 : list) {
                jedis.del("departments",String.valueOf(department1.getId()));
            }
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean update(Department department) {
        try {
            departmentDao.update(department);
            List<Department> list=departmentDao.queryPidNotZero();
            for (Department department1 : list) {
                jedis.del("departments",String.valueOf(department1.getId()));
            }

            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean del(int id) {
        try {
            departmentDao.del(id);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public List<Department> queryPidZero() {
        try {
            Department department = new Department();
            department.setName("无");
            department.setId(0);
            List<Department> list = departmentDao.queryPidZero();
            list.add(department);
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Department> queryPidDZero() {
        try {
            return departmentDao.queryPidDZero();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Department getByDid(int id) {
        try {
            return departmentDao.getByDid(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Department getById(int depid) {
        try {
            return departmentDao.getById(depid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<StaffsCount> getStaffsData() {
        try {
            return departmentDao.getStaffsData();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Department> findByPid(int pid) {
        try {
            return departmentDao.findByPid(pid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    @Override
    public List<Department> queryPidNotZero() {
        try {
            ObjectMapper mapper = new ObjectMapper();
            List<String> departments = jedis.hvals("departments");
            List<Department> list = new ArrayList<>();
            if (departments!=null&&departments.size()>0){//有数据
                for (String json : departments) {//把json转为Department对象
                    Department department = mapper.readValue(json, Department.class);
                    list.add(department);
                }
                return list;
            }else{//redis中，查询数据库
                List<Department> departmentList = departmentDao.queryPidNotZero();
                //存入redis中
                if (departmentList!=null && departmentList.size()>0){
                    for (Department department : departmentList) {
                        jedis.hset("departments",String.valueOf(department.getId()),mapper.writeValueAsString(department));
                        //设置过期时间
                        jedis.expire("departments",60*60L);
                    }
                }else{//缓存空值
                    jedis.hset("departments","","");
                }
                return queryPidNotZero();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Department> queryAll(){
        try {
            return departmentDao.queryAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
